﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.TestServer
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.ServiceModel;
    using System.ServiceModel.Description;

    /// <summary>Contains the entry point of the program.</summary>
    [ServiceBehavior]
    internal static class Program
    {
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Console.WriteLine(System.String)", Justification = "Will not be localized.")]
        private static void Main()
        {
            using (var fakeServerHost =
                new ServiceHost(typeof(FakeServer), new Uri("http://localhost:42042/PhuseFakeServer")))
            using (var nntpTestServerHost =
                new ServiceHost(typeof(NntpTestServer), new Uri("http://localhost:42042/PhuseNntpTestServer")))
            {
                // If this throws an AddressAccessDeniedException, the url must be registered on a command prompt with
                // Admin rights, as follows:
                // netsh http add urlacl url=http://+:42042/PhuseFakeServer/ user=THUNDERBOLT\Andreas
                Open(fakeServerHost);

                // If this throws an AddressAccessDeniedException, the url must be registered on a command prompt with
                // Admin rights, as follows:
                // netsh http add urlacl url=http://+:42042/PhuseNntpTestServer/ user=THUNDERBOLT\Andreas
                Open(nntpTestServerHost);

                Console.WriteLine("Press <Enter> to stop the service.");

                while (true)
                {
                    var line = Console.ReadLine();

                    switch (line.ToUpperInvariant())
                    {
                        case "START":
                            new NntpTestServer().Start(119);
                            break;
                        case "STOP":
                            new NntpTestServer().Stop();
                            break;
                        default:
                            return;
                    }
                }
            }
        }

        private static void Open(ServiceHost host)
        {
            // Enable metadata publishing.
            var smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);
            host.Open();
        }
    }
}
